﻿using System.Collections.ObjectModel;
using System.Windows.Data;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.IndexPart.ViewModels
{
    using Models;
    using Songhay.Mvvm.ViewModels;

    /// <summary>
    /// Master-Detail View Model
    /// </summary>
    public class MasterDetailViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MasterDetailViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public MasterDetailViewModel()
        {
            MasterDetailData.Load();
            var collection = new ObservableCollection<MasterDetailDatumViewModel>(MasterDetailData.DataItems);
            this.DataItemCollection = collection;
            this.DataItems = new PagedCollectionView(collection);

            this.ToggleDetailCommand = new RelayCommand(() =>
            {
                this.IsDetailVisible = !this.IsDetailVisible;

                Messenger.Default.Send<bool>(this.IsDetailVisible);
            });
        }

        /// <summary>
        /// Gets or sets the data items.
        /// </summary>
        /// <value>The data items.</value>
        public PagedCollectionView DataItems
        {
            get { return this._dataView; }
            set
            {
                this._dataView = value;
                base.RaisePropertyChanged("DataItems");
            }
        }

        /// <summary>
        /// Gets or sets the data item collection.
        /// </summary>
        /// <value>The data item collection.</value>
        public ObservableCollection<MasterDetailDatumViewModel> DataItemCollection
        {
            get { return this._dataItemCollection; }
            set
            {
                this._dataItemCollection = value;
                base.RaisePropertyChanged("DataItemCollection");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is detail visible.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is detail visible; otherwise, <c>false</c>.
        /// </value>
        public bool IsDetailVisible
        {
            get { return this._isDetailVisible; }
            set
            {
                this._isDetailVisible = value;
                base.RaisePropertyChanged("IsDetailVisible");
            }
        }

        /// <summary>
        /// Gets or sets the toggle detail command.
        /// </summary>
        /// <value>The toggle detail command.</value>
        public RelayCommand ToggleDetailCommand { get; private set; }

        /// <summary>
        /// Gets the toggle display text.
        /// </summary>
        /// <value>The toggle display text.</value>
        public string ToggleDisplayText { get { return this.IsDetailVisible ? "-" : "+"; } }

        bool _isDetailVisible;
        PagedCollectionView _dataView;
        ObservableCollection<MasterDetailDatumViewModel> _dataItemCollection;
    }
}
